/*
 * @Author: 涵崽
 * @Date: 2024-03-05 11:53:12
 * @email: 1045654@qq.com
 * @gitee: https://gitee.com/han-zai
 * @LastEditors: 涵崽
 * @LastEditTime: 2024-03-05 12:01:14
 * @Description: 微信：1045654
 */
const WebSocketPlugin = {
  install(Vue, options) {
    const socket = new WebSocket(options.url);

    Vue.prototype.$socket = socket;

    socket.addEventListener("open", () => {
      console.log("WebSocket连接成功");
    });

    socket.addEventListener("message", (event) => {
      console.log("Received message:", event.data);
      Vue.prototype.$emit("websocket-message", event.data);
    });

    socket.addEventListener("close", (event) => {
      console.log("WebSocket连接关闭", event);
      Vue.prototype.$emit("websocket-close", event);
    });

    socket.addEventListener("error", (error) => {
      console.error("WebSocket连接错误", error);
      Vue.prototype.$emit("websocket-error", error);
    });
  },
};

export default WebSocketPlugin;
